package com.servlet;

import com.dao.imp.ChangeGoodType;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/22 20:12
 **/

@WebServlet(name = "ChansgeCategoryNameServlet", value = "/ChansgeCategoryNameServlet")
public class ChansgeCategoryNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("categoryId"));
        String newCategoryName = request.getParameter("newCategoryName");
        String sql;
        String jsonString;
        if (id>900){
            sql = "UPDATE parenttype SET parent_type =\'"+newCategoryName+"\' WHERE parent_typeId="+id;
        }else {
            sql = "UPDATE childtype SET child_type =\'"+newCategoryName+"\' WHERE child_typeId="+id;
        }
        System.out.println(sql);
        ChangeGoodType changeGoodType = new ChangeGoodType();
        boolean changeResult = changeGoodType.updateGoodType(sql);
//        System.out.println(changeResult);
        if (changeResult){
            jsonString = "{status:0,msg:'update success'}";
        }else {
            jsonString = "{status:0,msg:'update failed'}";
        }
        JSONObject json = JSONObject.fromObject(jsonString);
        response.getWriter().print(json);
    }
}
